home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Draw Editor / Source / Promise.cpp < prev    next >
Encoding:
Text File  |  1995-12-11  |  8.4 KB  |  350 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Promise.cpp
  3.  
  4.     Contains:    Promise Classes Implementation
  5.  
  6.     Written by:    Dave Stafford
  7.     
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. // -- Compiler/Preprocessor Switches --
  12.  
  13. #ifndef _COMPILERDEFS_
  14. #include "CompDefs.h"
  15. #endif
  16.  
  17. // -- DrawEditor Includes --
  18.  
  19. #ifndef _SHAPES_
  20. #include "Shapes.h"
  21. #endif
  22.  
  23. #ifndef _STORUTIL_
  24. #include <StorUtil.h>
  25. #endif
  26.  
  27. #ifndef _ORDCOLL_
  28. #include "OrdColl.h"
  29. #endif
  30.  
  31. #ifndef _SELECTION_
  32. #include "Selection.h"
  33. #endif
  34.  
  35. #ifndef _PROMISE_
  36. #include "Promise.h"
  37. #endif
  38.  
  39. #ifndef _DRAWEDITOR_
  40. #include "DrawEditor.h"
  41. #endif
  42.  
  43. #ifndef _DRAWEDITORUTILS_
  44. #include "DrawEditorUtils.h"
  45. #endif
  46.  
  47. #ifndef _DRAWEDITORCONSTANTS_
  48. #include "DrawEditorConstants.h"
  49. #endif
  50.  
  51. #ifndef _DRAWEDITORGLOBALS_
  52. #include "DrawEditorGlobals.h"
  53. #endif
  54.  
  55. #ifndef _SAMPLECOLLECTIONS_
  56. #include "SampleCollections.h"
  57. #endif
  58.  
  59. // -- OpenDoc Includes --
  60.  
  61. #ifndef SOM_ODShape_xh
  62. #include <Shape.xh>
  63. #endif
  64.  
  65. #ifndef SOM_ODFacet_xh
  66. #include <Facet.xh>
  67. #endif
  68.  
  69. #ifndef SOM_ODFrame_xh
  70. #include <Frame.xh>
  71. #endif
  72.  
  73. #ifndef SOM_ODStorageUnit_xh
  74. #include <StorageU.xh>
  75. #endif
  76.  
  77. #ifndef SOM_ODSession_xh
  78. #include <ODSessn.xh>
  79. #endif
  80.  
  81. #ifndef SOM_ODPart_xh
  82. #include <Part.xh>
  83. #endif
  84.  
  85. // -- OpenDoc Utilities --
  86.  
  87. #ifndef _ODMEMORY_
  88. #include "ODMemory.h"            // ODDisposePtr
  89. #endif
  90.  
  91. #ifndef _ODDEBUG_
  92. #include "ODDebug.h"
  93. #endif
  94.  
  95. #ifndef _FOCUSLIB_
  96. #include <FocusLib.h>
  97. #endif
  98.  
  99. // -- Toolbox Includes --
  100.  
  101. #ifndef mathRoutinesIncludes
  102. #include <math routines.h>
  103. #endif
  104.  
  105. #ifndef __MEMORY__
  106. #include <memory.h>
  107. #endif
  108.  
  109.  
  110.  
  111. // **************************** Need to improve Handling of Failure **********************
  112.  
  113.  
  114. //=============================================================================
  115. // CPromise
  116. //=============================================================================
  117.  
  118. //----------------------------------------------------------------------------------------
  119. // CPromise::CPromise
  120. //----------------------------------------------------------------------------------------
  121.  
  122. CPromise::CPromise(DrawEditor* editor, 
  123.                     ODFrame* sourceFrame, 
  124.                     CSelection* selection, 
  125.                     ODCloneKind kind)
  126. {
  127.     fDrawEditor = editor;
  128.     fSourceFrame = sourceFrame;
  129.     fCloneKind = kind;
  130.     
  131.     // copies the content lists
  132.     fPromiseContent = new CPromiseContent(selection->GetSelectionContent());
  133. }
  134.  
  135.  
  136. //----------------------------------------------------------------------------------------
  137. // CPromise::~CPromise
  138. //----------------------------------------------------------------------------------------
  139.  
  140. CPromise::~CPromise()
  141. {
  142.     delete fPromiseContent;
  143. }
  144.  
  145.  
  146. //----------------------------------------------------------------------------------------
  147. // CPromise::Promise
  148. //----------------------------------------------------------------------------------------
  149.  
  150. void CPromise::Promise(Environment *ev, ODStorageUnit* storageUnit)
  151. {
  152.     CPromise* buffer = this;
  153.     ODByteArray promiseByteArray;
  154.     promiseByteArray._length = sizeof(CPromise*);
  155.     promiseByteArray._maximum = sizeof(CPromise*);
  156.     promiseByteArray._buffer = (octet*)&buffer;
  157.             
  158.     ODPart* part = fDrawEditor->GetODPart();
  159.     
  160.     // Must use a clone transaction, even to write a promise
  161.     ODDraft* fromDraft = fDrawEditor->GetDraft(ev);
  162.     ODDraftKey key = kODNULLID; 
  163.  
  164.     TRY
  165.         key = fromDraft->BeginClone(ev, storageUnit->GetDraft(ev), 
  166.                                                             fSourceFrame, fCloneKind);
  167.         storageUnit->SetPromiseValue(ev, fDrawEditor->GetEditorKind(), 0, 
  168.                                                             &promiseByteArray, part);
  169.         fromDraft->EndClone(ev, key);
  170.     CATCH_ALL
  171.         if (key!=kODNULLID)
  172.             fromDraft->AbortClone(ev, key);
  173.     ENDTRY
  174. }
  175.  
  176.  
  177.  
  178.  
  179. //----------------------------------------------------------------------------------------
  180. // CPromise::FulFillPromise
  181. //----------------------------------------------------------------------------------------
  182.  
  183. void CPromise::FulfillPromise(Environment* ev, ODStorageUnit* storage)
  184. {
  185.     COrderedList* shapes = kODNULL;
  186.     
  187.     ODDraft* fromDraft = fDrawEditor->GetDraft(ev);
  188.     
  189.     // Define clone information
  190.     CCloneInfo info(0, fromDraft, fSourceFrame, fCloneKind);    
  191.     
  192.     TRY
  193.         ODDraft* dstDraft = storage->GetDraft(ev);
  194.         
  195.         fPromiseContent->Externalize(ev, storage, &info);
  196.     CATCH_ALL
  197.         RERAISE;
  198.     ENDTRY
  199. }
  200.  
  201. //----------------------------------------------------------------------------------------
  202. // CPromise::FulFillPromise
  203. //----------------------------------------------------------------------------------------
  204.  
  205. void CPromise::FulfillPromise(Environment* ev, ODStorageUnitView* view)
  206. {
  207.     this->FulfillPromise(ev, view->GetStorageUnit(ev));
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. // CPromise::GetShapeList
  212. //----------------------------------------------------------------------------------------
  213.  
  214. COrderedList* CPromise::GetShapeList()
  215. {
  216.     return fPromiseContent->GetShapeList();
  217. }
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // CPromise::GetPromiseContent
  221. //----------------------------------------------------------------------------------------
  222. CDrawContent* CPromise::GetPromiseContent()
  223. {
  224.     return fPromiseContent;
  225. }
  226.  
  227. #pragma segment Main
  228. CPromise::CPromise(DrawEditor* editor, 
  229.                     ODFrame* sourceFrame, 
  230.                     COrderedList* shapeList,
  231.                     ODCloneKind kind)
  232. {
  233.     fDrawEditor = editor;
  234.     fSourceFrame = sourceFrame;
  235.     fCloneKind = kind;
  236.     
  237.     fPromiseContent = new CPromiseContent(editor, shapeList);
  238. }
  239.  
  240.  
  241.  
  242.  
  243.  
  244. //=============================================================================
  245. // CClipboardPromise
  246. //=============================================================================
  247. //----------------------------------------------------------------------------------------
  248. // CClipboardPromise::CClipboardPromise
  249. //----------------------------------------------------------------------------------------
  250.  
  251. CClipboardPromise::CClipboardPromise(DrawEditor* editor, 
  252.                     ODFrame* sourceFrame, 
  253.                     CSelection* selection, 
  254.                     ODCloneKind kind,
  255.                     ODUpdateID updateID) : CPromise( editor, sourceFrame, selection, kind )
  256. {
  257.     fUpdateID = updateID;
  258. }
  259.  
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // CClipboardPromise::~CClipboardPromise
  263. //----------------------------------------------------------------------------------------
  264.  
  265. CClipboardPromise::~CClipboardPromise()
  266. {
  267. }
  268.  
  269.  
  270. //----------------------------------------------------------------------------------------
  271. // CClipboardPromise::Promise
  272. //----------------------------------------------------------------------------------------
  273.  
  274. void CClipboardPromise::Promise(Environment *ev, ODStorageUnit* storageUnit)
  275. {
  276.     CPromise::Promise(ev, storageUnit);
  277.     
  278.     // Check the global promise, if its not null delete it
  279.     if (gGlobals->fClipboardPromise)
  280.     {
  281.         // Mark shapes no longer promised to the clipboard
  282.          gGlobals->fClipboardPromise->ShapesPromisedToClipboard(ev, kODFalse);
  283.         
  284.         delete gGlobals->fClipboardPromise;
  285.     }
  286.     
  287.     // Remember this promise, in case it never gets fulfilled so we can
  288.     // clean up the storage
  289.     gGlobals->fClipboardPromise = this;
  290.     
  291.     // Mark shapes as promised to the clipboard
  292.     this->ShapesPromisedToClipboard(ev, kODTrue);
  293. }
  294.  
  295.  
  296.  
  297.  
  298. //----------------------------------------------------------------------------------------
  299. // CClipboardPromise::ShapesPromisedToClipboard
  300. //----------------------------------------------------------------------------------------
  301.  
  302. void CClipboardPromise::ShapesPromisedToClipboard(Environment *ev, ODBoolean promised)
  303. {
  304.     COrdListIterator iter(fPromiseContent->GetShapeList());
  305.     for (CShape* shape = (CShape*)iter.First();
  306.         iter.IsNotComplete(); 
  307.         shape = (CShape*)iter.Next())
  308.     {
  309.         shape->SetPromisedToClipboard(promised);
  310.     }
  311. }
  312.  
  313.  
  314. //----------------------------------------------------------------------------------------
  315. // CClipboardPromise::FulFillPromise
  316. //----------------------------------------------------------------------------------------
  317.  
  318. void CClipboardPromise::FulfillPromise(Environment* ev, ODStorageUnit* storage)
  319. {
  320.     // Our parent method could throw, we need to make sure we get a chance to
  321.     // execute even if it does throw.
  322.     TRY
  323.         // Call inherited
  324.         CPromise::FulfillPromise(ev, storage);
  325.         
  326.         this->ShapesPromisedToClipboard(ev, kODFalse);
  327.  
  328.     CATCH_ALL
  329.     ENDTRY
  330.  
  331.     // If this promise isn't the one in gGlobals->fClipboardPromise, something is awry
  332.     if (gGlobals->fClipboardPromise!=this)
  333.     {
  334.         DebugStr("\pThis should not happen.");
  335.     }
  336.     else
  337.         // When a promise to the clip is fulfilled, the global should be set to null
  338.         // This must be done whether or not the base class method succeeds or fails.
  339.         gGlobals->fClipboardPromise = kODNULL;
  340.  
  341. }
  342.  
  343.  
  344. #pragma segment Main
  345. ODUpdateID CClipboardPromise::GetUpdateID()
  346. {
  347.     return fUpdateID;
  348. }
  349.  
  350.